package com.zy.base.network.retrofit2;

import android.media.MediaPlayer;

import com.zy.MyConfig;
import com.zy.base.network.Interctptor.LoggingInterceptor;

import java.util.concurrent.TimeUnit;

import dagger.Module;
import dagger.Provides;
import dagger.hilt.InstallIn;
import dagger.hilt.components.SingletonComponent;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

@Module
@InstallIn(SingletonComponent.class)
public class HiltRetrofit2 {
    @Provides
    public MediaPlayer getMediaPlayer() {
        return new MediaPlayer();
    }

    @Provides
    public Retrofit getRetrofit() {
        LoggingInterceptor.Builder builder = new LoggingInterceptor.Builder();
        builder.addHeader("Content_Type", "application/json")
                .addHeader("charset", "UTF-8");
        builder.loggable(true);
        OkHttpClient httpClient = new OkHttpClient.Builder()
//                .addInterceptor(interceptor)
                .addInterceptor(builder.build())
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(20, TimeUnit.SECONDS)
                .retryOnConnectionFailure(false)
                .build();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(MyConfig.HOST_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(httpClient)
                .build();

        return retrofit;
    }
}
